//
//  NSString+WDLMD5.m
//  24-加密
//
//  Created by 王道林 on 16/8/16.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "NSString+WDLMD5.h"
#import <CommonCrypto/CommonCrypto.h>
@implementation NSString (WDLMD5)

- (NSString *)stringWith32BitMD5Lower
{
    
    return [[self stringWith32BitMD5] lowercaseString];
}

- (NSString *)stringWith32BitMD5
{
    // 1 . 将字符串转化成C语言字符串
    const char *cString = [self UTF8String];
    CC_LONG length = (CC_LONG)strlen(cString);
    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
    // 2 . 将C的字符串转化 MB5
    CC_MD5(cString, length, bytes);
    // 3, 将C的字符串转化成OC的字符串
    NSMutableString *string = [NSMutableString stringWithCapacity:CC_MD5_BLOCK_LONG * 2];
    for (int i = 0; i < CC_MD5_BLOCK_LONG; i++) {
        [string appendFormat:@"%02x", bytes[i]];
    }
    return string;
}

- (NSString *)stringWith32BitMD5Upper
{
    return [[self stringWith32BitMD5] uppercaseString];
}



#pragma mark - 16位


- (NSString *)stringWith16BitMD5Lower
{
    return [[self stringWith16BitMD5] lowercaseString];
}

- (NSString *)stringWith16BitMD5Upper
{
    return [[self stringWith16BitMD5] uppercaseString];
}
//
- (NSString *)stringWith16BitMD5
{
//    // 1 . 将字符串转化成C语言字符串
//    const char *cString = [self UTF8String];
//    CC_LONG length = (CC_LONG)strlen(cString);
//    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
//    // 2 . 将C的字符串转化 MB5
//    CC_MD5(cString, length, bytes);
//    // 3, 将C的字符串转化成OC的字符串
//    NSMutableString *string = [NSMutableString stringWithCapacity:CC_MD5_BLOCK_LONG * 2];
//    for (int i = 4; i < CC_MD5_BLOCK_LONG - 3; i++) {
//        [string appendFormat:@"%02x", bytes[i]];
//    }
//    return string;
    
    
    //另一种方法.字符串拼接
    
    NSString *string = [self stringWith32BitMD5];
    NSString *str = [string substringFromIndex:8];
    return [str substringToIndex:str.length - 8];

}


@end
